<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Connect to a MySQL server</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.mysql-xdevapi-expression.html">« expression</a></li>
      <li style="float: right;"><a href="class.mysql-xdevapi-baseresult.html">mysql_xdevapi\BaseResult »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.mysql-xdevapi.html">Mysql_xdevapi 函数</a></li>
    <li>Connect to a MySQL server</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mysql-xdevapi-getsession" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">getSession</h1>
  <p class="verinfo">(No version information available, might only be in Git)</p><p class="refpurpose"><span class="refname">getSession</span> &mdash; <span class="dc-title">Connect to a MySQL server</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.mysql-xdevapi-getsession-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mysql_xdevapi\getSession</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$uri</code></span>): <span class="type"><a href="class.mysql-xdevapi-session.html" class="type mysql_xdevapi\Session">mysql_xdevapi\Session</a></span></div>

  <p class="para rdfs-comment">
   Connects to the MySQL server.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mysql-xdevapi-getsession-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">uri</code></dt>

    <dd>

     <p class="para">
      The URI to the MySQL server, such as <code class="literal">mysqlx://user:password@host</code>.
     </p>
     <p class="para">
      URI format:
     </p>
     <p class="para">
      <code class="code">scheme://[user[:[password]]@]target[:port][?attribute1=value1&amp;attribute2=value2...</code>
     </p>
     <ul class="itemizedlist">
      <li class="listitem">
        <p class="para"><code class="literal">scheme</code>: required, the connection protocol</p>
        <p class="para">In mysql_xdevapi it is always &#039;mysqlx&#039; (for X Protocol)</p>
      </li>
      <li class="listitem">
        <p class="para"><code class="literal">user</code>: optional, the MySQL user account for authentication</p>
      </li>
      <li class="listitem">
        <p class="para"><code class="literal">password</code>: optional, the MySQL user&#039;s password for authentication</p>
      </li>
      <li class="listitem">
        <p class="para"><code class="literal">target</code>: required, the server instance the connection refers to:</p>
        <p class="para">* TCP connection (host name, IPv4 address, or IPv6 address)</p>
        <p class="para">* Unix socket path (local file path)</p>
        <p class="para">* Windows named-pipe (local file path)</p>
      </li>
      <li class="listitem">
        <p class="para"><code class="literal">port</code>: optional, network port of MySQL server.</p>
        <p class="para">by default port for X Protocol is 33060</p>
      </li>
      <li class="listitem">
        <p class="para">
          <code class="literal">?attribute=value</code>: this element is optional and specifies a data dictionary 
          that contains different options, including:
        </p>
        <ul class="itemizedlist">
          <li class="listitem">
          <p class="para">
            The <code class="literal">auth</code> (authentication mechanism) attribute as it relates to encrypted connections.
            For additional information, see <a href="https://dev.mysql.com/doc/refman/8.0/en/encrypted-connection-options.html" class="link external">&raquo;&nbsp;Command
            Options for Encrypted Connections</a>.
            The following &#039;auth&#039; values are supported: <code class="literal">plain</code>,
            <code class="literal">mysql41</code>, <code class="literal">external</code>, and <code class="literal">sha256_mem</code>.
          </p>
          </li>
          <li class="listitem">
          <p class="para">
           The <code class="literal">connect-timeout</code> attribute affects the connection
           and not subsequent operations. It is set per connection whether on
           a single or multiple hosts.
          </p>
          <p class="para">
           Pass in a positive integer to define the connection timeout in seconds,
           or pass in 0 (zero) to disable the timeout (infinite). Not defining
           connect-timeout uses the default value of 10.
          </p>
          <p class="para">
            Related, the MYSQLX_CONNECTION_TIMEOUT (timeout in seconds) and MYSQLX_TEST_CONNECTION_TIMEOUT (used while running tests)
            environment variables can be set and used instead of connect-timeout in the URI. The
            connect-timeout URI option has precedence over these environment variables.
          </p>
          </li>
          <li class="listitem">
            <p class="para">
              The optional <code class="literal">compression</code> attribute accepts these values: 
              <code class="literal">preferred</code> (client negotiates with server to find a supported algorithm; connection is uncompressed if a mutually supported algorithm is not found), 
              <code class="literal">required</code> (like &quot;preferred&quot;, but connection is terminated if a mutually supported algorithm is not found), or
              <code class="literal">disabled</code> (connection is uncompressed). Defaults to <code class="literal">preferred</code>.
            </p>
            <p class="para">
              This option was added in version 8.0.20.
            </p>
          </li>
          <li class="listitem">
            <p class="para">
              The optional <code class="literal">compression-algorithms</code> attribute defines 
              the desired compression algorithms (and their preferred usage order): 
              <code class="literal">zstd_stream</code> (alias: zstd),
              <code class="literal">lz4_message</code> (alias: lz4), or
              <code class="literal">deflate_stream</code> (aliases: deflate or zlib).
              By default, the order used (depending on system availability) is lz4_message, zstd_stream, then deflate_stream.
              For example, passing in compression-algorithms=[lz4,zstd_stream] uses lz4 if it&#039;s available, otherwise
              zstd_stream is used. If both are unavailable then behavior depends on the compression value
              e.g., if compression=required then it&#039;ll fail with an error.
            </p>
            <p class="para">
              This option was added in version 8.0.22.
            </p>
          </li>
        </ul>
       </li>
      </ul>

  <div class="example" id="example-1551">
  <p><strong>示例 #1 URI examples</strong></p>
  <div class="example-contents"><div class="phpcode"><code><span style="color: #000000">
mysqlx://foobar<br />mysqlx://root@localhost?socket=%2Ftmp%2Fmysqld.sock%2F<br />mysqlx://foo:bar@localhost:33060<br />mysqlx://foo:bar@localhost:33160?ssl-mode=disabled<br />mysqlx://foo:bar@localhost:33260?ssl-mode=required<br />mysqlx://foo:bar@localhost:33360?ssl-mode=required&amp;auth=mysql41<br />mysqlx://foo:bar@(/path/to/socket)<br />mysqlx://foo:bar@(/path/to/socket)?auth=sha256_mem<br />mysqlx://foo:bar@[localhost:33060,&nbsp;127.0.0.1:33061]<br />mysqlx://foobar?ssl-ca=(/path/to/ca.pem)&amp;ssl-crl=(/path/to/crl.pem)<br />mysqlx://foo:bar@[localhost:33060,&nbsp;127.0.0.1:33061]?ssl-mode=disabled<br />mysqlx://foo:bar@localhost:33160/?connect-timeout=0<br />mysqlx://foo:bar@localhost:33160/?connect-timeout=10&amp;compression=required<br />mysqlx://foo:bar@localhost:33160/?connect-timeout=10&amp;compression=required&amp;compression-algorithms=[lz4,zstd_stream]</span>
</code></div></div>

</div>

   <p class="para">
    For related information, see MySQL Shell&#039;s
    <a href="https://dev.mysql.com/doc/refman/8.0/en/mysql-shell-connection-using-uri.html" class="link external">&raquo;&nbsp;Connecting using a URI String</a>.
   </p>

    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mysql-xdevapi-getsession-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   A <span class="classname"><strong class="classname">Session</strong></span> object.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.mysql-xdevapi-getsession-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   A connection failure throws an <span class="classname"><a href="class.exception.html" class="classname">Exception</a></span>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mysql-xdevapi-getsession-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1552">
   <p><strong>示例 #2 <span class="function"><strong>mysql_xdevapi\getSession()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$session&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_xdevapi</span><span style="color: #007700">\</span><span style="color: #0000BB">getSession</span><span style="color: #007700">(</span><span style="color: #DD0000">"mysqlx://user:password@host"</span><span style="color: #007700">);<br />}&nbsp;catch(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Connection&nbsp;could&nbsp;not&nbsp;be&nbsp;established:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #0000BB">$schemas&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getSchemas</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$schemas</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$mysql_version&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getServerVersion</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$mysql_version</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">(</span><span style="color: #DD0000">"name&nbsp;=&nbsp;'Alfred'"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">fetchOne</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; mysql_xdevapi\Schema Object
        (
            [name] =&gt; helloworld
        )
    [1] =&gt; mysql_xdevapi\Schema Object
        (
            [name] =&gt; information_schema
        )
    [2] =&gt; mysql_xdevapi\Schema Object
        (
            [name] =&gt; mysql
        )
    [3] =&gt; mysql_xdevapi\Schema Object
        (
            [name] =&gt; performance_schema
        )
    [4] =&gt; mysql_xdevapi\Schema Object
        (
            [name] =&gt; sys
        )
)

80012

array(4) {
  [&quot;_id&quot;]=&gt;
  string(28) &quot;00005ad66abf0001000400000003&quot;
  [&quot;age&quot;]=&gt;
  int(42)
  [&quot;job&quot;]=&gt;
  string(7) &quot;Butler&quot;
  [&quot;name&quot;]=&gt;
  string(4) &quot;Alfred&quot;
}
</pre></div>
   </div>
  </div>
 </div>



</div></div></div></body></html>